package cn.xiaohuodui.service;

import cn.xiaohuodui.constant.ErrorCodeDefine;
import cn.xiaohuodui.dao.CategoryMapper;
import cn.xiaohuodui.dic.CategoryLevel;
import cn.xiaohuodui.exceptions.BussException;
import cn.xiaohuodui.form.back.category.BackCategoryQueryForm;
import cn.xiaohuodui.model.Category;
import cn.xiaohuodui.res.JsonRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by TR on 2018/3/30.
 */
@Service
public class CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    public List<Category>  queryL1(){
        return categoryMapper.getCategoriesByLevel(CategoryLevel.L1.getDesc());
    }

    public Category findById(Integer id) throws BussException {
        Category category = categoryMapper.selectByPrimaryKey(id);
        if(category==null){
            throw new BussException(ErrorCodeDefine.CATEGORY_NOT_EXIST.getCode(),ErrorCodeDefine.CATEGORY_NOT_EXIST.getDesc());
        }
        return category;
    }


    //递归算法，求某一类目下的所有子孙类目
//    public Set<Category> querySubsByPid(Set<Category> categories, Integer id){
//        Category category = categoryMapper.selectByPrimaryKey(id);
//        if(category!=null){
//            categories.add(category);
//            List<Category> childrenByPid = categoryMapper.getChildrensByPid(id);
//            for(Category category1:childrenByPid){
//                querySubsByPid(categories,category1.getId());
//            }
//        }
//        return categories;
//    }
//

    public List<Category> querySubsByPid(Integer id){
        Category category = categoryMapper.selectByPrimaryKey(id);
        List<Category> childrenByPid = new ArrayList<>();
        if(category!=null){
            childrenByPid = categoryMapper.getChildrensByPid(id);
        }
        return childrenByPid;
    }


    /**
     * 获取分类栏目数据
     * 参数id是可选的，如果没有设置，则选择第一个一级目录
     * 这里的id应该是一级目录的id。
     */
    public JsonRes<Map<String,Object>> getCateData(Integer id) throws BussException {
        // 所有一级分类目录
        List<Category> l1CatList = this.queryL1();

        // 当前一级分类目录
        Category currentCategory = null;
        if(id != null){
            currentCategory = this.findById(id);
        }
        else{
            currentCategory = l1CatList.get(0);
        }

        // 当前一级分类目录对应的二级分类目录
//        Set<Category> currentSubCategory = new HashSet<>();
        List<Category> childrenByPid = new ArrayList<>();
        if (null != currentCategory) {
            childrenByPid = this.querySubsByPid(currentCategory.getId());
        }

        Map<String, Object> data = new HashMap();
        if(id == null){   //若id不为null,就不用传入一级类目list
            data.put("categoryList", l1CatList);
        }
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", childrenByPid);
        return JsonRes.createBySuccessData(data);
    }



















}
